import 'vue/jsx'

// 引入windi css
import '@/plugins/unocss'

// 导入全局的svg图标
import '@/plugins/svgIcon'

// 初始化多语言
import { setupI18n } from '@/plugins/vueI18n'

// 引入状态管理
import { setupStore } from '@/store'

// 全局组件
import { setupGlobCom } from '@/components'
import anvilComponents from './components/Zmids'

// 引入全局样式
import '@/styles/index.less'

// 引入动画
import '@/plugins/animate.css'

// 引入element-plus
import { setupElementPlus } from '@/plugins/elementPlus'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as Icons from '@element-plus/icons'

// 路由
import { setupRouter } from './router'

// 权限
import directives from './directives'

import { defineAsyncComponent, createApp } from 'vue'

import App from './App.vue'

import './permission'

// 本地SVG图标
import 'virtual:svg-icons-register'
import moment from 'moment'

// 引入全局方法
import { getCacheDict, getTableExtCol } from '@/platform/base/api/base/dict/type'
import {
  selectDictLabel,
  resetForm,
  downloadUrl,
  handleTree,
  parseTime,
  addDateRange,
  getAssetsUrl,
  getCustomUploadTrans
} from '@/utils/zmids'
import { getCacheConfig } from '@/platform/base/api/base/config'
import { hasPermi, hasRole, hasRoleType } from '@/utils/permission'
import * as wfClient from '@/platform/wf/api/wf/common/zbiti-wf'
// 分片上传组件
import uploader from 'vue-simple-uploader';
import 'vue-simple-uploader/dist/style.css';
// 引入低代码相关配置
import AnvilCodeLowCommon from '@/platform/code/components/anvil-code-low-common'
import AnvilCodeLowDesign from '@/platform/code/components/anvil-code-low-design'
// 引入数据开发相关配置
import DataDevelopmentCommon from '@/platform/data/api/data/dataDevelopment/common.js'
// 引入bpmnjs配置
import BpmnJs from '@/platform/wf/components/bpmnjs'

// 创建实例
const setupAll = async () => {
  const app = createApp(App)

  // 注册全局方法
  app.config.globalProperties.getDicts = getCacheDict
  app.config.globalProperties.getTableExtCol = getTableExtCol

  app.config.globalProperties.selectDictLabel = selectDictLabel
  app.config.globalProperties.resetForm = resetForm
  app.config.globalProperties.downloadUrl = downloadUrl
  app.config.globalProperties.handleTree = handleTree
  app.config.globalProperties.parseTime = parseTime
  app.config.globalProperties.addDateRange = addDateRange
  app.config.globalProperties.getAssetsUrl = getAssetsUrl
  app.config.globalProperties.getCustomUploadTrans = getCustomUploadTrans

  app.config.globalProperties.hasPermi = hasPermi
  app.config.globalProperties.hasRole = hasRole
  app.config.globalProperties.hasRoleType = hasRoleType
  app.config.globalProperties.getConfigKey = getCacheConfig

  app.config.globalProperties.wfClient = wfClient

  app.config.globalProperties.$moment = moment
  app.config.globalProperties.msgSuccess = function (msg: string) {
    app.config.globalProperties.$message({ showClose: true, message: msg, type: 'success' })
  }

  app.config.globalProperties.msgError = function (msg: string) {
    app.config.globalProperties.$message({ showClose: true, message: msg, type: 'error' })
  }

  app.config.globalProperties.msgWarning = function (msg: string) {
    app.config.globalProperties.$message({ showClose: true, message: msg, type: 'warning' })
  }

  // 全局捕获组件级报错
  app.config.errorHandler = (err, vm, info) => {
    const errMsg = '组件级错误: ' + err.message
    app.config.globalProperties.msgError(errMsg)
    console.error('errorHandler', err)
  }

  // Anvil自定义组件
  for (const comp in anvilComponents) {
    // app.component(comp, anvilComponents[comp])
    app.component(comp, defineAsyncComponent(anvilComponents[comp]))
  }

  const loadPlugins = async () => {
    await setupI18n(app)
    setupGlobCom(app)
    setupElementPlus(app)
    // 注册element-plus和图标
    app.use(ElementPlus)
    // element-plus图标
    for (const [key, component] of Object.entries(Icons)) {
      app.component(key, component)
    }
    app.use(directives)
    app.use(uploader)
    app.use(AnvilCodeLowCommon)
    app.use(AnvilCodeLowDesign)
    app.use(DataDevelopmentCommon)  
    app.use(BpmnJs)
  }

  setupStore(app)
  setupRouter(app)
  app.mount('#app')
  // 先挂载再异步加载配置
  loadPlugins()
}

setupAll()
